home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 May (DVD) / Macworld Resource DVD May 2003.toast / Data / Shareware / Internet / Blosxom1.2.sit / Blosxom.pkg / Contents / Resources / blosxom.cgi < prev    next >
Encoding:
Text File  |  2003-02-13  |  10.6 KB  |  283 lines

  1. #!/usr/bin/perl
  2.  
  3. # Blosxom
  4. # Author: Rael Dornfest <rael@oreilly.com>
  5. # Version: 1.2
  6. # Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
  7.  
  8. # --- Configurable variables -----
  9.  
  10. # What's this blog's title?
  11. my $blog_title = "My Blosxom";
  12.  
  13. # What's this blog's description (for outgoing RSS feed)?
  14. my $blog_description = "Yet another Blosxom blog.";
  15.  
  16. # What's this blog's primary language (for outgoing RSS feed)?
  17. my $blog_language = "en";
  18.  
  19. # Where are this blog's entries kept?
  20. my $datadir = "/Library/WebServer/Documents/blosxom";
  21.  
  22. # What's my preferred base URL for this blog (leave blank for automatic)?
  23. my $url = "";
  24.  
  25. # Should I stick only to the datadir for items or travel down the
  26. # directory hierarchy looking for items?  If so, to what depth?
  27. # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down
  28. my $depth = 0;
  29.  
  30. # How many entries should I show on the home page?
  31. my $num_entries = 40;
  32.  
  33. # What file extension signifies a blosxom entry?
  34. my $file_extension = "txt";
  35.  
  36. # What is the default flavour?
  37. my $default_flavour = "html";
  38.  
  39. # --- Static Rendering -----
  40.  
  41. # Where are this blog's static files to be created?
  42. my $static_dir = "/Library/WebServer/Documents/blog";
  43.  
  44. # What's my administrative password (you must set this for static rendering)?
  45. my $static_password = "";
  46.  
  47. # What flavours should I generate statically?
  48. my @static_flavours = qw/html rss/;
  49.  
  50. # Should I statically generate individual entries?
  51. # 0 = no, 1 = yes
  52. my $static_entries = 0;
  53.  
  54. # --------------------------------
  55.  
  56. use strict;
  57. use FileHandle;
  58. use File::Find;
  59. use File::stat;
  60. use Time::localtime;
  61. use CGI qw/:standard :netscape/;
  62.  
  63. my %month2num = (nil=>'00', Jan=>'01', Feb=>'02', Mar=>'03', Apr=>'04', May=>'05', Jun=>'06', Jul=>'07', Aug=>'08', Sep=>'09', Oct=>'10', Nov=>'11', Dec=>'12');
  64. my @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
  65.  
  66. # Use the stated preferred URL or figure it out automatically
  67. $url ||= url();
  68. $url =~ s/^included:/http:/; # Fix for Server Side Includes (SSI)
  69. $url =~ s!/$!!;
  70.   
  71. # Fix depth to take into account datadir's path
  72. $depth and $depth += ($datadir =~ tr[/][]) - 1;
  73.  
  74. # Global variable to be used in head/foot.{flavour} templates
  75. my $path_info = '';
  76.  
  77. my $fh = new FileHandle;
  78.  
  79. # Bring in the templates
  80. my %template = ();
  81. while (<DATA>) {
  82.   last if /^(__END__)?$/;
  83.   my($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/;
  84.   $txt =~ s/\\n/\n/mg;
  85.   $template{$ct}{$comp} = $txt;
  86. }
  87.  
  88. my ($d, %files, %indexes, %entries);
  89. find(
  90.   sub { 
  91.     my $curr_depth = $File::Find::dir =~ tr[/][]; 
  92.     return if $depth and $curr_depth > $depth; 
  93.   
  94.     $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
  95.       and $2 ne 'index' and $2 !~ /^\./
  96.         and $files{$File::Find::name} = stat($File::Find::name)->mtime
  97.           and (
  98.             param('-all') 
  99.             or !-T "$static_dir/$1/index." . $static_flavours[0]
  100.             or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime
  101.           )
  102.             and $indexes{$1} = 1
  103.               and $d = join('/', (nice_date(stat($File::Find::name)->mtime))[5,2,3])
  104.                 and $indexes{$d} = $d
  105.                   and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1;
  106.  
  107.   }, 
  108.   $datadir
  109. );
  110.  
  111. # Static
  112. if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) {
  113.  
  114.   param('-quiet') or print "Blosxom is generating static index pages...\n";
  115.  
  116.   # Home Page and Directory Indexes
  117.   my %done;
  118.   foreach my $path ( sort keys %indexes) {
  119.     my $p = '';
  120.     foreach ( ('', split /\//, $path) ) {
  121.       $p .= "/$_";
  122.       $p =~ s!^/!!;
  123.       $done{$p}++ and next;
  124.       (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755;
  125.       foreach my $flavour ( @static_flavours ) {
  126.         chomp(my $content_type = $fh->open("< $datadir/$p/content_type.$flavour") || $fh->open("< $datadir/content_type.$flavour") ? <$fh> : ($template{$flavour}{'content_type'} || 'text/plain'));
  127.         my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
  128.         param('-quiet') or print "$fn.$flavour\n";
  129.         my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!";  
  130.         print $fh_w 
  131.           $indexes{$path} == 1
  132.             ? &generate('static', $p, '', $flavour, $content_type)
  133.             : &generate('static', '', $p, $flavour, $content_type);
  134.         $fh_w->close;
  135.       }
  136.     }
  137.   }
  138. }
  139.  
  140. # Dynamic
  141. else {
  142.   # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
  143.   my @path_info = split m{/}, path_info(); 
  144.   shift @path_info;
  145.  
  146.   while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; }
  147.  
  148.   # An icky bit of backward-compatibility with Blosxom < 0+5i; remove the # to enable
  149.   #path_info() =~ m!/xml/?$! and $flavour = 'rss';
  150.  
  151.   # Flavour specified by ?flav={flav} or index.{flav}
  152.   my $flavour;
  153.   if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
  154.     $flavour = $2;
  155.     $1 ne 'index' and $path_info .= "/$1.$2";
  156.     pop @path_info;
  157.   } else {
  158.     $flavour = param('flav') || $default_flavour;
  159.   }
  160.  
  161.   $path_info =~ s!^/!!;
  162.  
  163.   # Date fiddling
  164.   my($path_info_yr,$path_info_mo,$path_info_da) = @path_info;
  165.   my $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef;
  166.  
  167.   chomp(my $content_type = $fh->open("< $datadir/$path_info/content_type.$flavour") || $fh->open("< $datadir/content_type.$flavour") ? <$fh> : ($template{$flavour}{'content_type'} || 'text/plain'));
  168.   print header($content_type);
  169.  
  170.   print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type);
  171. }
  172.  
  173. # Generate 
  174. sub generate {
  175.   my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_;
  176.   my $return = '';
  177.  
  178.   # Header
  179.   my $head = join '', ($fh->open("< $datadir/$currentdir/head.$flavour") || $fh->open("< $datadir/head.$flavour") ? <$fh> : ($template{$flavour}{'head'} || $template{'error'}{'head'}));
  180.   $head =~ s/(\$\w+)/$1 . "||''"/gee;
  181.   $return .= $head;
  182.   
  183.   # Stories
  184.   my $curdate = '';
  185.   my $ne = $num_entries;
  186.  
  187.   my %f;
  188.   if ( $currentdir =~ /(.+?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
  189.     $currentdir = "$1$2.$file_extension";
  190.     $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} );
  191.   } 
  192.   else { 
  193.     $currentdir =~ s!/index\..+$!!;
  194.     %f = %files;
  195.   }
  196.  
  197.   foreach my $path_file ( sort { $files{$b} <=> $files{$a} } keys %f ) {
  198.     last if $ne <= 0 && $date !~ /\d/;
  199.     my($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
  200.  
  201.     # Only stories in the right hierarchy
  202.     $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next;
  203.  
  204.     # Prepend a slash for use in templates only if a path exists
  205.     $path &&= "/$path";
  206.  
  207.     # Date fiddling for by-{year,month,day} archive views
  208.     my ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date(stat("$path_file")->mtime);
  209.     my ($hr,$min) = split /:/, $ti;
  210.  
  211.     # Only stories from the right date
  212.     my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date;
  213.     next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr; 
  214.     next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
  215.     next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da; 
  216.    
  217.     # Date display 
  218.     if ( $curdate ne "$da/$mo/$yr" ) {
  219.       my $date = join '', ($fh->open("< $datadir/$path/date.$flavour") || $fh->open("< $datadir/date.$flavour") ? <$fh> : ($template{$flavour}{'date'} || $template{'html'}{'date'}));
  220.       $date =~ s/(\$\w+)/$1 . "||''"/gee;
  221.       $curdate = "$da/$mo/$yr";
  222.       $return .= $date;
  223.     }
  224.     
  225.     if (-T "$path_file" && $fh->open("< $path_file")) {
  226.       chomp(my $title = <$fh>);
  227.       chomp(my $body = join '', <$fh>);
  228.       
  229.       if ($content_type =~ m{\Wxml$}) {
  230.         # Escape <, >, and &, and to produce valid RSS
  231.         my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');  
  232.         my $escape_re  = join '|' => keys %escape;
  233.         $title =~ s/($escape_re)/$escape{$1}/g;
  234.         $body =~ s/($escape_re)/$escape{$1}/g;
  235.       }
  236.       
  237.       $fh->close;
  238.       my $story = join '', ($fh->open("< $datadir/$path/story.$flavour") || $fh->open("< $datadir/story.$flavour") ? <$fh> : ($template{$flavour}{'story'} || $template{'error'}{'story'}));
  239.       $story =~ s/(\$\w+)/$1 . "||''"/gee;
  240.       $return .= $story;
  241.       $fh->close;
  242.  
  243.       $ne--;
  244.     }
  245.   }
  246.  
  247.   # Footer
  248.   my $foot = join '', ($fh->open("< $datadir/$currentdir/foot.$flavour") || $fh->open("< $datadir/foot.$flavour") ? <$fh> : ($template{$flavour}{'foot'} || $template{'error'}{'foot'}));
  249.   $foot =~ s/(\$\w+)/$1 . "||''"/gee;
  250.   $return .= $foot;
  251.   
  252.   $return;
  253. }
  254.  
  255.  
  256. sub nice_date {
  257.   my($unixtime) = @_;
  258.   
  259.   my $c_time = ctime($unixtime);
  260.   my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ );
  261.   $da = sprintf("%02d", $da);
  262.   my $mo_num = $month2num{$mo};
  263.   
  264.   return ($dw,$mo,$mo_num,$da,$ti,$yr);
  265. }
  266.  
  267.  
  268. # Default HTML and RSS template bits
  269. __DATA__
  270. html content_type text/html
  271. html head <html><head><link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/?flav=rss" /><title>$blog_title $path_info_da $path_info_mo $path_info_yr</title></head><body><center><font size="+3">$blog_title</font><br />$path_info_da $path_info_mo $path_info_yr</center><p />
  272. html story <p><a name="$fn"><b>$title</b></a><br />$body<br /><br />posted at: $ti | path: <a href="$url$path">$path</a> | <a href="$url/$yr/$mo_num/$da#$fn">permanent link to this entry</a></p>\n
  273. html date <h3>$dw, $da $mo $yr</h3>\n
  274. html foot <p /><center><a href="http://www.raelity.org/apps/blosxom/"><img src="http://www.raelity.org/images/pb_blosxom.gif" border="0" /></body></html>
  275. rss content_type text/xml
  276. rss head <?xml version="1.0"?>\n<!-- name="generator" content="bloxsom/1.1" -->\n<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">\n\n<rss version="0.91">\n  <channel>\n    <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>\n    <link>$url</link>\n    <description>$blog_description</description>\n    <language>$blog_language</language>\n
  277. rss story   <item>\n    <title>$title</title>\n    <link>$url/$yr/$mo_num/$da#$fn</link>\n    <description>$body</description>\n  </item>\n
  278. rss date \n
  279. rss foot   </channel>\n</rss>
  280. error head Error: I'm afraid this is the first I've heard of a "$flavour" flavoured Blosxom.  Try dropping the "/+$flavour" bit from the end of the URL.\n\n
  281. html content_type text/html
  282. __END__
  283.